#include "Animation.h"
#include "bitmap.h"
#include "Engine.h"
#include "memoryDef.h"
#include "display.h"


CAnimation::CAnimation(CEngine *engine)
:m_frameWidth(0),
m_frameHeight(0),
m_frameNum(0),
m_curFrame(0),
m_idleTick(0),
m_frameTime(FRAME_TIME),
m_frameImg(engine)
{
	m_engine = engine;
}

CAnimation::~CAnimation()
{
}

void CAnimation::LoadAnim(char *filename, int width, int height, int num)
{
	m_frameWidth = width;
	m_frameHeight = height;
	m_frameNum = num;
	m_frameImg.Loadbmp(filename);
	m_frameImg.SetColorKey(0, 0, 0);
}

void CAnimation::DrawAnimation(int posX, int posY, bool loop)
{
	if(!IsAnimEnd())
	{
		m_frameImg.Drawbmp32ColorKey(m_curFrame*m_frameWidth, 0, m_frameWidth, m_frameHeight, posX, posY);
		if(m_frameTime-- <= 0 && m_engine->m_key.IsAnyKeyHold())
		{
			++m_curFrame;
			m_frameTime = FRAME_TIME;
		}
	}
	else
	{
		if(loop)
		{
			m_curFrame = 0;
			m_frameImg.Drawbmp32ColorKey(m_curFrame*m_frameWidth, 0, m_frameWidth, m_frameHeight, posX, posY);
			++m_curFrame;
		}
	}
}

void CAnimation::DrawIdle(int posX, int posY/*, bool loop*/)
{
	if(m_curFrame < 5 && m_curFrame >6)
		m_curFrame = 5;

	m_idleTick++;
	if(m_idleTick > 10)
	{
		m_curFrame = 6;
	}
	if(m_idleTick > 20)
	{
		m_idleTick = 0;
		m_curFrame = 5;
	}
	m_frameImg.Drawbmp32ColorKey(m_curFrame*m_frameWidth, 0, m_frameWidth, m_frameHeight, posX, posY);
}

bool CAnimation::IsAnimEnd()
{
	return m_curFrame >= m_frameNum? true: false;
}

void CAnimation::ResetAnim()
{
	m_curFrame = 0;
}